www.gusucode.com > VC++下的串行数据通信以及八个串口编程实例- > VC++下的串行数据通信以及八个串口编程实例-/第7章 智能安防报警系统/ChjDlg.cpp
// ChjDlg.cpp : implementation file // #include "stdafx.h" #include "Alert.h" #include "ChjDlg.h" #include "SetPara2Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CChjDlg dialog CChjDlg::CChjDlg(CWnd* pParent /*=NULL*/) : CDialog(CChjDlg::IDD, pParent) { //{{AFX_DATA_INIT(CChjDlg) m_chjCode = _T(""); m_chjName = _T(""); m_chjPort = _T(""); m_chjTelNum = _T(""); //}}AFX_DATA_INIT } void CChjDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CChjDlg) DDX_Text(pDX, IDC_EDITCODE, m_chjCode); DDX_Text(pDX, IDC_EDITNAME, m_chjName); DDX_Text(pDX, IDC_EDITPORT, m_chjPort); DDX_Text(pDX, IDC_EDITTELNUM, m_chjTelNum); //}}AFX_DATA_MAP if(pDX->m_bSaveAndValidate) { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO1); if(pButton->GetCheck()) m_chjProperty="串口"; else { pButton=(CButton*)GetDlgItem(IDC_RADIO2); if(pButton->GetCheck()) m_chjProperty="并口"; else m_chjProperty=""; } pButton=(CButton*)GetDlgItem(IDC_RADIO3); if(pButton->GetCheck()) m_nActType=1; //发码 else { pButton=(CButton*)GetDlgItem(IDC_RADIO4); if(pButton->GetCheck()) m_nActType=2; //拨号 else m_nActType=0; } } else { if(m_chjProperty=="串口") { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO1); pButton->SetCheck(TRUE); } if(m_chjProperty=="并口") { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO2); pButton->SetCheck(TRUE); } if(m_nActType==1) { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO3); pButton->SetCheck(TRUE); } if(m_nActType==2) { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO4); pButton->SetCheck(TRUE); } } } BOOL CChjDlg::OnInitDialog() { CSetPara2Dlg* pWnd=(CSetPara2Dlg*)GetParent(); m_nAct = pWnd->m_nAct; if(m_nAct==1) //修改动作,初始化参数 { //初始化bfname,bfcode,porperty,number等 pWnd->m_chjList.GetItemText(pWnd->m_nSel,0,m_chjName.GetBuffer(100),100); pWnd->m_chjList.GetItemText(pWnd->m_nSel,1,m_chjProperty.GetBuffer(100),100); pWnd->m_chjList.GetItemText(pWnd->m_nSel,2,m_chjPort.GetBuffer(100),100); m_chjName.ReleaseBuffer(); m_chjProperty.ReleaseBuffer(); m_chjPort.ReleaseBuffer(); CString strActType; pWnd->m_chjList.GetItemText(pWnd->m_nSel,3,strActType.GetBuffer(100),100); strActType.ReleaseBuffer(); if(strActType=="发码") { m_nActType=1; pWnd->m_chjList.GetItemText(pWnd->m_nSel,4,m_chjCode.GetBuffer(100),100); m_chjCode.ReleaseBuffer(); m_chjTelNum=""; } else if(strActType=="拨号") { m_nActType=2; m_chjCode=""; pWnd->m_chjList.GetItemText(pWnd->m_nSel,4,m_chjTelNum.GetBuffer(100),100); m_chjTelNum.ReleaseBuffer(); } else { m_chjCode=""; m_chjTelNum=""; } } CDialog::OnInitDialog(); // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BEGIN_MESSAGE_MAP(CChjDlg, CDialog) //{{AFX_MSG_MAP(CChjDlg) ON_EN_CHANGE(IDC_EDITPORT, OnChangeEditport) ON_EN_CHANGE(IDC_EDITTELNUM, OnChangeEdittelnum) ON_EN_CHANGE(IDC_EDITCODE, OnChangeEditcode) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CChjDlg message handlers BOOL CChjDlg::DataCheck() { //数据检查规则:判断form上数据是否完整 if((m_chjName=="") ||(m_chjPort=="") ||(m_chjProperty=="")) return FALSE; if(m_nActType!=1 && m_nActType!=2) return FALSE; if(m_nActType==1 && m_chjCode=="") return FALSE; if(m_nActType==2 && m_chjTelNum=="") return FALSE; return TRUE; } void CChjDlg::AddNew() { //新加一条记录 _RecordsetPtr r; r.CreateInstance(_uuidof(Recordset)); CString s="select * from chujingset"; _bstr_t bs=s.AllocSysString(); TRY { CAlertApp* pApp=(CAlertApp*)::AfxGetApp(); r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText); r->AddNew(); FillRecord(r); r->Update(); //将list更新 CSetPara2Dlg* pWnd=(CSetPara2Dlg*)GetParent(); int nItem = pWnd->m_chjList.InsertItem(pWnd->m_chjList.GetItemCount(),m_chjName); pWnd->m_chjList.SetItemText(nItem,1,m_chjProperty); pWnd->m_chjList.SetItemText(nItem,2,m_chjPort); CString act; if(m_nActType==1) { act="发码"; pWnd->m_chjList.SetItemText(nItem,4,m_chjCode); pWnd->m_chjList.SetItemText(nItem,5,"-"); } else if(m_nActType==2) { act="拨号"; pWnd->m_chjList.SetItemText(nItem,4,"-"); pWnd->m_chjList.SetItemText(nItem,5,m_chjTelNum); } else { act="-"; pWnd->m_chjList.SetItemText(nItem,4,"-"); pWnd->m_chjList.SetItemText(nItem,5,"-"); } pWnd->m_chjList.SetItemText(nItem,3,act); } CATCH(CException,e) { e->ReportError(); ::AfxMessageBox("请再次检查数据"); } END_CATCH r->Close(); r=NULL; } void CChjDlg::Editbf() { _RecordsetPtr r; r.CreateInstance(_uuidof(Recordset)); CSetPara2Dlg* pWnd=(CSetPara2Dlg*)GetParent(); CString tempcjName; pWnd->m_chjList.GetItemText(pWnd->m_nSel,0,tempcjName.GetBuffer(100),100); tempcjName.ReleaseBuffer(); CString s="select * from chujingset where cjname = '" +tempcjName + "'"; _bstr_t bs=s.AllocSysString(); TRY { CAlertApp* pApp=(CAlertApp*)::AfxGetApp(); r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText); FillRecord(r); r->Update(); //将list更新 CSetPara2Dlg* pWnd=(CSetPara2Dlg*)GetParent(); pWnd->m_chjList.SetItemText(pWnd->m_nSel,0,m_chjName); pWnd->m_chjList.SetItemText(pWnd->m_nSel,1,m_chjProperty); pWnd->m_chjList.SetItemText(pWnd->m_nSel,2,m_chjPort); CString act; if(m_nActType==1) { act="发码"; pWnd->m_chjList.SetItemText(pWnd->m_nSel,4,m_chjCode); pWnd->m_chjList.SetItemText(pWnd->m_nSel,5,"-"); } else if(m_nActType==2) { act="拨号"; pWnd->m_chjList.SetItemText(pWnd->m_nSel,4,"-"); pWnd->m_chjList.SetItemText(pWnd->m_nSel,5,m_chjTelNum); } else { act="-"; pWnd->m_chjList.SetItemText(pWnd->m_nSel,4,"-"); pWnd->m_chjList.SetItemText(pWnd->m_nSel,5,"-"); } pWnd->m_chjList.SetItemText(pWnd->m_nSel,3,act); } CATCH(CException,e) { ::AfxMessageBox("请再次检查数据"); e->ReportError(); } END_CATCH r->Close(); r=NULL; } void CChjDlg::FillRecord(_RecordsetPtr r) { r->PutCollect("cjname",m_chjName.AllocSysString()); r->PutCollect("property",m_chjProperty.AllocSysString()); r->PutCollect("number",m_chjPort.AllocSysString()); CString act; if(m_nActType==1) { act="发码"; r->PutCollect("infocode",m_chjCode.AllocSysString()); } else if(m_nActType==2) { act="拨号"; long telnumber=atoi(m_chjTelNum.GetBuffer(50)); m_chjTelNum.ReleaseBuffer(); r->PutCollect("telnumber",COleVariant(telnumber)); r->PutCollect("infocode",CString("-").AllocSysString()); } else act="-"; r->PutCollect("typeact",act.AllocSysString()); } void CChjDlg::OnOK() { // TODO: Add extra validation here if (!UpdateData(TRUE)) { TRACE0("UpdateData failed during dialog termination.\n"); // the UpdateData routine will set focus to correct item return; } //数据检查并给以提示 if(!DataCheck()) { ::AfxMessageBox("数据不完整或不符合要求,请检查"); return; } //根据不同操作采取不同的action if(m_nAct==0) { AddNew(); } if(m_nAct==1) { Editbf(); } EndDialog(IDOK); } void CChjDlg::OnChangeEditport() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here CString NUM="0123456789"; CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITPORT); CString str; pEdit->GetWindowText(str); //判断端口编号是否为数字 for(int i=0;i<str.GetLength();i++) { if(NUM.Find(str[i])==-1) { ::AfxMessageBox("端口编号应为数字"); pEdit->SetWindowText(str.Left(i)); return; } } //如果端口编号是数字的话再分别按照端口类型判断 //option1:串行端口 CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO1); if(pButton->GetCheck()) { if(atoi(str.GetBuffer(50))>16) { ::AfxMessageBox("串口编号范围在0-16之间"); pEdit->SetWindowText(""); } str.ReleaseBuffer(); } pButton=(CButton*)GetDlgItem(IDC_RADIO2); if(pButton->GetCheck()) { if(atoi(str.GetBuffer(50))>65535) { ::AfxMessageBox("并口编号范围在0-65535之间"); pEdit->SetWindowText(""); } str.ReleaseBuffer(); } } void CChjDlg::OnChangeEdittelnum() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here CString NUM="0123456789"; CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITTELNUM); CString str; pEdit->GetWindowText(str); for(int i=0; i<str.GetLength();i++) { if(NUM.Find(str[i])==-1) { ::AfxMessageBox("电话号码应该为数字"); pEdit->SetWindowText(str.Left(i)); break; } } } void CChjDlg::OnChangeEditcode() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here CString NUM="0123456789"; CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITCODE); CString str; pEdit->GetWindowText(str); for(int i=0; i<str.GetLength();i++) { if(NUM.Find(str[i])==-1) { ::AfxMessageBox("信号码应该为十进制数"); pEdit->SetWindowText(str.Left(i)); break; } } }